package com.witsight.platform.filter;

import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.AbstractMatcherFilter;
import ch.qos.logback.core.spi.FilterReply;

/**   
 * 说明：[]
 * @Title: MarkerFilter.java 
 * @Package com.witsight.platform.filter 
 * @See: []<br/>
 * Copyright: Copyright (c) 2017<br/>
 * Company:sany huax witsight team by product
 * @author: penght  
 * @date: 2017年11月1日 下午2:17:13 
 * @version: V1.0
 *
 */
public class MarkerFilter extends AbstractMatcherFilter<ILoggingEvent> {
	Marker markerToMatch;

	/*
	 * (non-Javadoc)
	 * 
	 * @see ch.qos.logback.core.filter.Filter#decide(java.lang.Object)
	 */
	@Override
	public FilterReply decide(ILoggingEvent event) {
		if (!isStarted()) {
			return FilterReply.NEUTRAL;
		}
		Marker marker = event.getMarker();
		if (marker == null) {
			return onMismatch;
		}

		if (markerToMatch.contains(marker)) {
			return onMatch;
		} else {
			return onMismatch;
		}
	}

	/**
	 * The marker to match in the event.
	 * 
	 * @param markerToMatch
	 */
	public void setMarker(String markerStr) {
		if (markerStr != null) {
			this.markerToMatch = MarkerFactory.getMarker(markerStr);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ch.qos.logback.core.filter.Filter#start()
	 */
	@Override
	public void start() {
		if (this.markerToMatch != null) {
			super.start();
		}
	}
}
